package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.ChangeHouseConfirmBean;
import com.supply.dl.bean.ChangeHouseConfirmDetailBean;
import com.supply.dl.bean.MaterielBean;
import com.supply.dl.bean.WorkInfoBean;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.Utils;
import com.supply.dl.view.MaxRecyclerView;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.MaterielAdapter;
import com.supply.dl.widget.adapter.WorkDetailsAdapter;

import java.util.ArrayList;

/**
 * 移库收货确认详情
 */
public class ChangeHouseAndConfirmDetailActivity extends BaseActivity implements MaterielAdapter.OnItemClickListener
{
	private ChangeHouseConfirmBean changeHouseConfirmBean;
	@BindView(R.id.txt_notice_code) public TextView txtNotifyCode;   //移库单号
	@BindView(R.id.txt_notice_status) public TextView txtNoticeStatus;  //订单状态
	@BindView(R.id.txt_notice_time) public TextView txtNoticeTime;  //建单时间
	@BindView(R.id.txt_order_num) public TextView txtOrderNum;//订单号
	@BindView(R.id.txt_notice_out) TextView txtOut;  //移除仓库
	@BindView(R.id.txt_notice_in) TextView txtIn; //移入仓库
	@BindView(R.id.txt_notice_type) TextView txtType;//移库类型
	@BindView(R.id.maxRecyclerView) MaxRecyclerView wuLiaoRecyclerView;//物料名称横向列表
	@BindView(R.id.wuliao_detail) MaxRecyclerView wuLiaoDetailRecyclerView;// 物料明细列表
	@BindView(R.id.common_materiel_position_info_showFirstValue) TextView txtWuLiaoCode; //物料编码
	@BindView(R.id.common_materiel_position_info_showSecondValue) TextView txtWuName; //物料名称
	@BindView(R.id.common_materiel_position_info_showThirdValue) TextView txtWuTotal;//物料总数
	@BindView(R.id.common_materiel_position_info_showFourthValue) TextView txtWorkNum;//以作业数量
	@BindView(R.id.txt_Pass) public TextView txtPass;  //过账按钮
	public static final String NOTICE_ORDER_NUM = "order_id";
	private MaterielAdapter materielAdapter;
	private WorkDetailsAdapter workDetailListAdapter;
	private String orderNum;
	public ArrayList<MaterielBean> noticeOrderDetailList = new ArrayList<MaterielBean>();//物料信息json数组
	@BindView(R.id.layout_load_failed) LoadTipFailedLayout loadTipFailedLayout;
	public ArrayList<WorkInfoBean> jobInfoList = new ArrayList<WorkInfoBean>(); //作业

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_confirm_details;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mActivity = this;
		setTitleShow("移库收货详情");
		showLeftBack();
		Intent intent = getIntent();
		orderNum = intent.getStringExtra(NOTICE_ORDER_NUM);
		changeHouseConfirmBean = (ChangeHouseConfirmBean) intent.getSerializableExtra("ChangeHouseConfirmBean");
		initBaseInfo(changeHouseConfirmBean);
		txtPass.setOnClickListener(this);
		//初始化物料横向列表
		{
			materielAdapter = new MaterielAdapter(this, noticeOrderDetailList, this, 0);
			LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
			linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
			wuLiaoRecyclerView.setLayoutManager(linearLayoutManager);
			wuLiaoRecyclerView.setAdapter(materielAdapter);
		}
		//初始化作业列表
		{
			workDetailListAdapter = new WorkDetailsAdapter(this, jobInfoList);
			LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
			linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
			wuLiaoDetailRecyclerView.setLayoutManager(linearLayoutManager);
			wuLiaoDetailRecyclerView.setAdapter(workDetailListAdapter);
		}
		initData(orderNum);
		loadTipFailedLayout.setRestartOnClickListener(this);
	}

	private void initBaseInfo(ChangeHouseConfirmBean changeHouseConfirmBean)
	{
		txtNotifyCode.setText(changeHouseConfirmBean.orderNo);
		txtNoticeStatus.setText(changeHouseConfirmBean.noticeOrderStatusName);
		txtNoticeTime.setText("建单时间:" + (StringUtils.isEmpty(String.valueOf(changeHouseConfirmBean.addTime)) ? "" :
				DateUtils.getSimpleDateTimeByMill(String.valueOf(changeHouseConfirmBean.addTime), DateUtils.DATE_TIME_FORMAT)));
		txtOrderNum.setText("通知单号:" + changeHouseConfirmBean.noticeOrderNo);
		txtType.setText("移库单类型:" + changeHouseConfirmBean.orderTypeName);
		txtOut.setText("移除仓库:" + changeHouseConfirmBean.tfOutWhName);
		txtIn.setText("移入仓库:" + changeHouseConfirmBean.tfOutWhName);
	}

	/**
	 * 初始化数据
	 */
	public void initData(String noticeOrderId)
	{
		loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		httpManger.getConfirmDetails(noticeOrderId);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				ChangeHouseConfirmDetailBean changeHouseConfirmDetailBean = (ChangeHouseConfirmDetailBean) baseInfo.getData();
				if (changeHouseConfirmDetailBean != null)
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
					noticeOrderDetailList.clear();
					noticeOrderDetailList.addAll(changeHouseConfirmDetailBean.noticeOrderDetailList);
					materielAdapter.notifyDataSetChanged();

					jobInfoList.clear();
					if (noticeOrderDetailList.size() > 0)
					{
						initWListUI(noticeOrderDetailList.get(0));
						jobInfoList.addAll(noticeOrderDetailList.get(0).jobInfoList);
						workDetailListAdapter.notifyDataSetChanged();
					}
				}
				else
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
				}
			}

			@Override public void onFailure(String message)
			{
				showToast(message);
				loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
			}
		});
	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{
		case R.id.txt_load_failed:
			initData(orderNum);
			break;
		case R.id.txt_Pass:
			passBill(orderNum);
			break;
		}
	}

	/**
	 * 初始化物料list
	 */
	private void initWListUI(MaterielBean materielBean)
	{
		txtWuLiaoCode.setText(materielBean.skuCode);
		txtWuName.setText(materielBean.skuName);
		txtWuTotal.setText(materielBean.volume);
		txtWorkNum.setText(materielBean.jobAmount + "件/" + materielBean.jobWeight + "吨/" + materielBean.jobVolume + "方");
	}

	private void passBill(String noticeOrderId)
	{
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		httpManger.receivingGoodsConfirmDetailPostingAccount(noticeOrderId);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				Toast.makeText(mActivity, "过账成功", Toast.LENGTH_SHORT).show();
				finish();
			}

			@Override public void onFailure(String message)
			{
				Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override public void onItemClickListener(int position)
	{
		materielAdapter.setPosition(position);
		initWListUI(noticeOrderDetailList.get(position));
		jobInfoList.addAll(noticeOrderDetailList.get(position).jobInfoList);
		workDetailListAdapter.notifyDataSetChanged();
	}
}
